/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.dto.ShopSubagentDto;
import com.yami.shop.bean.model.ShopSubagent;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopSubagentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 下级代理列表控制器
 */
@RestController
@RequestMapping("/shop/subagent")
@Api(tags = "下级代理列表接口")
public class ShopSubagentController {

    @Autowired
    private ShopSubagentService shopSubagentService;

    /**
     * 分页获取下级代理列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页获取下级代理列表", notes = "分页获取当前店铺的下级代理列表")
    public ResponseEntity<IPage<ShopSubagentDto>> getShopSubagentPage(PageParam<ShopSubagent> page) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        IPage<ShopSubagentDto> subagentPage = shopSubagentService.getShopSubagentsWithDetailByShopId(
                new Page<>(page.getCurrent(), page.getSize()), shopId);
        return ResponseEntity.ok(subagentPage);
    }

    /**
     * 添加下级代理
     */
    @PostMapping
    @ApiOperation(value = "添加下级代理", notes = "添加下级代理店铺")
    public ResponseEntity<Void> addShopSubagent(@RequestBody ShopSubagent shopSubagent) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopSubagent.setShopId(shopId);
        
        // 验证下级店铺ID不能为空
        if (shopSubagent.getSubShopId() == null) {
            throw new YamiShopBindException("下级店铺ID不能为空");
        }
        
        // 验证不能添加自己为下级代理
        if (shopId.equals(shopSubagent.getSubShopId())) {
            throw new YamiShopBindException("不能添加自己为下级代理");
        }
        
        boolean result = shopSubagentService.addShopSubagent(shopSubagent);
        if (!result) {
            throw new YamiShopBindException("该下级代理已存在");
        }
        
        return ResponseEntity.ok().build();
    }

    /**
     * 更新下级代理审核状态
     */
    @PutMapping("/audit/{id}")
    @ApiOperation(value = "更新下级代理审核状态", notes = "更新下级代理的审核状态")
    public ResponseEntity<Void> updateAuditStatus(
            @ApiParam(value = "下级代理ID") @PathVariable("id") Long id,
            @ApiParam(value = "审核状态 (0: 待审核 1: 已审核 -1: 已拒绝)") @RequestParam("auditStatus") Integer auditStatus,
            @ApiParam(value = "备注") @RequestParam(value = "remarks", required = false) String remarks) {
        
        boolean result = shopSubagentService.updateAuditStatus(id, auditStatus, remarks);
        if (!result) {
            throw new YamiShopBindException("更新审核状态失败");
        }
        
        return ResponseEntity.ok().build();
    }

    /**
     * 删除下级代理
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除下级代理", notes = "删除下级代理关系")
    public ResponseEntity<Void> deleteShopSubagent(
            @ApiParam(value = "下级代理ID") @PathVariable("id") Long id) {
        
        boolean result = shopSubagentService.deleteShopSubagent(id);
        if (!result) {
            throw new YamiShopBindException("删除下级代理失败");
        }
        
        return ResponseEntity.ok().build();
    }
} 